home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.4)
-
- import sys
- import os
- import marshal
- import imp
- import struct
- import time
- import zlib
- from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED
- from test import test_support
- from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co
- import zipimport
-
- def make_pyc(co, mtime):
- data = marshal.dumps(co)
- if type(mtime) is type(0.0):
- if mtime < 2147483647:
- mtime = int(mtime)
- else:
- mtime = int(-0x100000000L + long(mtime))
-
- pyc = imp.get_magic() + struct.pack('<i', int(mtime)) + data
- return pyc
-
- NOW = time.time()
- test_pyc = make_pyc(test_co, NOW)
- pyc_ext = '.pyo'
- TESTMOD = 'ziptestmodule'
- TESTPACK = 'ziptestpackage'
- TESTPACK2 = 'ziptestpackage2'
- TEMP_ZIP = os.path.abspath('junk95142' + os.extsep + 'zip')
-
- class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
- compression = ZIP_STORED
-
- def setUp(self):
- zipimport._zip_directory_cache.clear()
- ImportHooksBaseTestCase.setUp(self)
-
-
- def doTest(self, expected_ext, files, *modules, **kw):
- z = ZipFile(TEMP_ZIP, 'w')
-
- try:
- for mtime, data in files.items():
- zinfo = ZipInfo(name, time.localtime(mtime))
- zinfo.compress_type = self.compression
- z.writestr(zinfo, data)
-
- z.close()
- stuff = kw.get('stuff', None)
- if stuff is not None:
- f = open(TEMP_ZIP, 'rb')
- data = f.read()
- f.close()
- f = open(TEMP_ZIP, 'wb')
- f.write(stuff)
- f.write(data)
- f.close()
-
- sys.path.insert(0, TEMP_ZIP)
- mod = __import__('.'.join(modules), globals(), locals(), [
- '__dummy__'])
- if expected_ext:
- file = mod.get_file()
- self.assertEquals(file, os.path.join(TEMP_ZIP, *modules) + expected_ext)
- finally:
- z.close()
- os.remove(TEMP_ZIP)
-
-
-
- def testAFakeZlib(self):
- if 'zlib' in sys.builtin_module_names:
- return None
-
- if 'zlib' in sys.modules:
- del sys.modules['zlib']
-
- files = {
- 'zlib.py': (NOW, test_src) }
-
- try:
- self.doTest('.py', files, 'zlib')
- except ImportError:
- if self.compression != ZIP_DEFLATED:
- self.fail('expected test to not raise ImportError')
-
- except:
- self.compression != ZIP_DEFLATED
-
- if self.compression != ZIP_STORED:
- self.fail('expected test to raise ImportError')
-
-
-
- def testPy(self):
- files = {
- TESTMOD + '.py': (NOW, test_src) }
- self.doTest('.py', files, TESTMOD)
-
-
- def testPyc(self):
- files = {
- TESTMOD + pyc_ext: (NOW, test_pyc) }
- self.doTest(pyc_ext, files, TESTMOD)
-
-
- def testBoth(self):
- files = {
- TESTMOD + '.py': (NOW, test_src),
- TESTMOD + pyc_ext: (NOW, test_pyc) }
- self.doTest(pyc_ext, files, TESTMOD)
-
-
- def testEmptyPy(self):
- files = {
- TESTMOD + '.py': (NOW, '') }
- self.doTest(None, files, TESTMOD)
-
-
- def testBadMagic(self):
- m0 = ord(test_pyc[0])
- m0 ^= 4
- badmagic_pyc = chr(m0) + test_pyc[1:]
- files = {
- TESTMOD + '.py': (NOW, test_src),
- TESTMOD + pyc_ext: (NOW, badmagic_pyc) }
- self.doTest('.py', files, TESTMOD)
-
-
- def testBadMagic2(self):
- m0 = ord(test_pyc[0])
- m0 ^= 4
- badmagic_pyc = chr(m0) + test_pyc[1:]
- files = {
- TESTMOD + pyc_ext: (NOW, badmagic_pyc) }
-
- try:
- self.doTest('.py', files, TESTMOD)
- except ImportError:
- pass
-
- self.fail('expected ImportError; import from bad pyc')
-
-
- def testBadMTime(self):
- t3 = ord(test_pyc[7])
- t3 ^= 2
- badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:]
- files = {
- TESTMOD + '.py': (NOW, test_src),
- TESTMOD + pyc_ext: (NOW, badtime_pyc) }
- self.doTest('.py', files, TESTMOD)
-
-
- def testPackage(self):
- packdir = TESTPACK + os.sep
- files = {
- packdir + '__init__' + pyc_ext: (NOW, test_pyc),
- packdir + TESTMOD + pyc_ext: (NOW, test_pyc) }
- self.doTest(pyc_ext, files, TESTPACK, TESTMOD)
-
-
- def testDeepPackage(self):
- packdir = TESTPACK + os.sep
- packdir2 = packdir + TESTPACK2 + os.sep
- files = {
- packdir + '__init__' + pyc_ext: (NOW, test_pyc),
- packdir2 + '__init__' + pyc_ext: (NOW, test_pyc),
- packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc) }
- self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)
-
-
- def testGetData(self):
- z = ZipFile(TEMP_ZIP, 'w')
- z.compression = self.compression
-
- try:
- name = 'testdata.dat'
- data = []([ chr(x) for x in range(256) ]) * 500
- z.writestr(name, data)
- z.close()
- zi = zipimport.zipimporter(TEMP_ZIP)
- self.assertEquals(data, zi.get_data(name))
- finally:
- z.close()
- os.remove(TEMP_ZIP)
-
-
-
- def testImporterAttr(self):
- src = 'if 1: # indent hack\n def get_file():\n return __file__\n if __loader__.get_data("some.data") != "some data":\n raise AssertionError, "bad data"\n'
- pyc = make_pyc(compile(src, '<???>', 'exec'), NOW)
- files = {
- TESTMOD + pyc_ext: (NOW, pyc),
- 'some.data': (NOW, 'some data') }
- self.doTest(pyc_ext, files, TESTMOD)
-
-
- def testImport_WithStuff(self):
- files = {
- TESTMOD + '.py': (NOW, test_src) }
- self.doTest('.py', files, TESTMOD, stuff = 'Some Stuff' * 31)
-
-
-
- class CompressedZipImportTestCase(UncompressedZipImportTestCase):
- compression = ZIP_DEFLATED
-
-
- def test_main():
- test_support.run_unittest(UncompressedZipImportTestCase, CompressedZipImportTestCase)
-
- if __name__ == '__main__':
- test_main()
-
-